package com.cardone.platform.common.controller;

import java.util.List;
import java.util.Map;

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.cardone.common.dto.PaginationDto;
import com.cardone.context.Constants;
import com.cardone.platform.common.dto.EntityDefaultDto;
import com.cardone.platform.common.dto.EntityExtendDto;
import com.cardone.platform.common.dto.EntityLogDto;
import com.cardone.platform.common.dto.EntityRelationshipDto;
import com.cardone.platform.common.dto.EntityTreeDto;
import com.cardone.platform.common.service.CommonService;
import com.cardone.platform.common.validator.EntityDefaultValidator;
import com.cardone.platform.common.validator.EntityExtendValidator;
import com.cardone.platform.common.validator.EntityLogValidator;
import com.cardone.platform.common.validator.EntityRelationshipValidator;
import com.cardone.platform.common.validator.EntityTreeValidator;
import com.cardone.platform.common.vo.AssistantVo;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * 辅助
 * 
 * @author yaoht
 */
@Setter
@Slf4j
@RequestMapping(AssistantController.Paths.ROOT)
@Controller
public class AssistantController {
	/**
	 * models集合
	 * 
	 * @author yaoht
	 * 
	 */
	public enum models {
		/**
		 * 实体与默认
		 */
		entityDefault,

		/**
		 * 实体扩展
		 */
		entityExtend,

		/**
		 * 实体日志
		 */
		entityLog,

		/**
		 * 实体关系
		 */
		entityRelationship,

		/**
		 * 实体树
		 */
		entityTree,

		/**
		 * 消息
		 */
		message;
	}

	/**
	 * 路径集合
	 * 
	 * @author yaoht
	 * 
	 */
	public enum Paths {
		/**
		 * 实体与默认编辑视图路径
		 */
		ENTITY_DEFAULTEdit("entityDefault/edit"),

		/**
		 * 实体与默认主界面视图路径
		 */
		ENTITY_DEFAULTIndex("entityDefault/index"),

		/**
		 * 实体与默认编辑视图路径
		 */
		ENTITY_DEFAULTNew("entityDefault/new"),

		/**
		 * 实体与默认查看编辑视图路径
		 */
		ENTITY_DEFAULTView("entityDefault/view"),

		/**
		 * 实体扩展编辑视图路径
		 */
		ENTITY_EXTENDEdit("entityExtend/edit"),

		/**
		 * 实体扩展主界面视图路径
		 */
		ENTITY_EXTENDIndex("entityExtend/index"),

		/**
		 * 实体扩展编辑视图路径
		 */
		ENTITY_EXTENDNew("entityExtend/new"),

		/**
		 * 实体扩展查看编辑视图路径
		 */
		ENTITY_EXTENDView("entityExtend/view"),

		/**
		 * 实体日志编辑视图路径
		 */
		ENTITY_LOGEdit("entityLog/edit"),

		/**
		 * 实体日志主界面视图路径
		 */
		ENTITY_LOGIndex("entityLog/index"),

		/**
		 * 实体日志编辑视图路径
		 */
		ENTITY_LOGNew("entityLog/new"),

		/**
		 * 实体日志查看编辑视图路径
		 */
		ENTITY_LOGView("entityLog/view"),

		/**
		 * 实体关系编辑视图路径
		 */
		ENTITY_RELATIONSHIPEdit("entityRelationship/edit"),

		/**
		 * 实体关系主界面视图路径
		 */
		ENTITY_RELATIONSHIPIndex("entityRelationship/index"),

		/**
		 * 实体关系编辑视图路径
		 */
		ENTITY_RELATIONSHIPNew("entityRelationship/new"),

		/**
		 * 实体关系查看编辑视图路径
		 */
		ENTITY_RELATIONSHIPView("entityRelationship/view"),

		/**
		 * 实体树编辑视图路径
		 */
		ENTITY_TREEEdit("entityTree/edit"),

		/**
		 * 实体树主界面视图路径
		 */
		ENTITY_TREEIndex("entityTree/index"),

		/**
		 * 实体树编辑视图路径
		 */
		ENTITY_TREENew("entityTree/new"),

		/**
		 * 实体树查看编辑视图路径
		 */
		ENTITY_TREEView("entityTree/view"),

		/**
		 * 辅助主界面
		 */
		index;

		/**
		 * 辅助视图根目录
		 */
		public static final String ROOT = "/platform/common/assistant/";

		/**
		 * 路径
		 */
		private final String path;

		/**
		 * 构建
		 * 
		 */
		private Paths() {
			this.path = Paths.ROOT + this.name();
		}

		/**
		 * 构建
		 * 
		 * @param path
		 *            路径
		 */
		private Paths(final String path) {
			this.path = Paths.ROOT + path;
		}

		/**
		 * 获取
		 * 
		 * @return 路径
		 */
		public String path() {
			return this.path;
		}
	}

	/**
	 * 辅助服务
	 */
	@Qualifier(CommonService.BEAN_ID)
	@Autowired(required = false)
	private CommonService commonService;

	/**
	 * 实体与默认验证
	 */
	@Qualifier(EntityDefaultValidator.BEAN_ID)
	@Autowired(required = false)
	public Validator entityDefaultValidator;

	/**
	 * 实体扩展验证
	 */
	@Qualifier(EntityExtendValidator.BEAN_ID)
	@Autowired(required = false)
	public Validator entityExtendValidator;

	/**
	 * 实体日志验证
	 */
	@Qualifier(EntityLogValidator.BEAN_ID)
	@Autowired(required = false)
	public Validator entityLogValidator;

	/**
	 * 实体关系验证
	 */
	@Qualifier(EntityRelationshipValidator.BEAN_ID)
	@Autowired(required = false)
	public Validator entityRelationshipValidator;

	/**
	 * 实体树验证
	 */
	@Qualifier(EntityTreeValidator.BEAN_ID)
	@Autowired(required = false)
	public Validator entityTreeValidator;

	/**
	 * 实体与默认：删除
	 * 
	 * @param assistantVo
	 *            实体与默认视图对象
	 * @return 处理结果，json格式
	 */
	@RequestMapping(value = { "entityDefault/delete.json" }, method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> entityDefaultDelete(final AssistantVo assistantVo) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.commonService.getAssistantService().deleteEntityDefaultByIds(assistantVo.getEntityDefault());
		} catch (final Exception e) {
			jsonMap.put(Constants.message.stringValue(), e.getMessage());

			if (AssistantController.log.isDebugEnabled()) {
				AssistantController.log.debug(e.getMessage(), e);
			}
		}

		return jsonMap;
	}

	/**
	 * 实体与默认：编辑
	 * 
	 * @param assistantVo
	 *            实体与默认视图对象
	 * @param model
	 *            上下文映射
	 * @return 实体与默认编辑视图路径
	 */
	@RequestMapping(value = { "entityDefault/edit.html" }, method = RequestMethod.GET)
	public String entityDefaultEdit(final AssistantVo assistantVo, final Model model) {
		final EntityDefaultDto entityDefault = this.commonService.getAssistantService().findEntityDefaultById(
		        assistantVo.getEntityDefault());

		model.addAttribute(AssistantController.models.entityDefault.name(), entityDefault);

		return AssistantController.Paths.ENTITY_DEFAULTEdit.path();
	}

	/**
	 * 实体与默认：主界面
	 * 
	 * @return 实体与默认主界面视图路径
	 */
	@RequestMapping(value = { "entityDefault/index.html" })
	public String entityDefaultIndex() {
		return AssistantController.Paths.ENTITY_DEFAULTIndex.path();
	}

	/**
	 * 实体与默认：主界面分页查询
	 * 
	 * @param assistantVo
	 *            实体与默认视图对象
	 * @return 分页数据，json格式
	 */
	@RequestMapping(value = { "entityDefault/index/pagination.json" })
	@ResponseBody
	public Map<String, Object> entityDefaultIndexPagination(final AssistantVo assistantVo) {
		final PaginationDto<EntityDefaultDto> entityDefaultPagination = this.commonService.getAssistantService()
		        .paginationEntityDefaultByCode(assistantVo.getEntityDefault());

		final Map<String, Object> entityDefaultPaginationMap = Maps.newHashMap();

		entityDefaultPaginationMap.put(Constants.total.stringValue(), entityDefaultPagination.getTotalSize());
		entityDefaultPaginationMap.put(Constants.rows.stringValue(), entityDefaultPagination.getDataList());

		return entityDefaultPaginationMap;
	}

	/**
	 * 实体与默认：新增
	 * 
	 * @param assistantVo
	 *            实体与默认视图对象
	 * @param model
	 *            上下文映射
	 * @return 实体与默认编辑视图路径
	 */
	@RequestMapping(value = { "entityDefault/new.html" }, method = RequestMethod.GET)
	public String entityDefaultNew(final AssistantVo assistantVo, final Model model) {
		model.addAttribute(AssistantController.models.entityDefault.name(), assistantVo.getEntityDefault());

		return AssistantController.Paths.ENTITY_DEFAULTNew.path();
	}

	/**
	 * 实体与默认：保存
	 * 
	 * @param assistantVo
	 *            实体与默认视图对象
	 * @param bindingResult
	 *            绑定对象
	 * @return 处理结果，json格式
	 */
	@RequestMapping(value = { "entityDefault/save.txt" }, method = RequestMethod.POST)
	@ResponseBody
	public String entityDefaultSave(final AssistantVo assistantVo, BindingResult bindingResult) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.entityDefaultValidator.validate(assistantVo.getEntityDefault(), bindingResult);

			if (bindingResult.hasErrors()) {
				List<String> messageList = Lists.newArrayList();

				for (final ObjectError objectError : bindingResult.getAllErrors()) {
					messageList.add(objectError.getDefaultMessage());
				}

				String message = StringUtils.join(messageList, org.apache.commons.lang3.StringUtils.EMPTY);

				throw new Exception(message);
			}

			this.commonService.getAssistantService().saveEntityDefaultByIdOrCode(assistantVo.getEntityDefault());
		} catch (final Exception e) {
			jsonMap.put(Constants.message.stringValue(), e.getMessage());

			if (AssistantController.log.isDebugEnabled()) {
				AssistantController.log.debug(e.getMessage(), e);
			}
		}

		return JSON.toJSONString(jsonMap);
	}

	/**
	 * 实体与默认：更新
	 * 
	 * @param assistantVo
	 *            实体与默认视图对象
	 * @return 处理结果，json格式
	 */
	@RequestMapping(value = { "entityDefault/update.json" }, method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> entityDefaultUpdate(final AssistantVo assistantVo) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.commonService.getAssistantService().updateEntityDefaultByIds(assistantVo.getEntityDefault());
		} catch (final Exception e) {
			jsonMap.put(Constants.message.stringValue(), e.getMessage());

			if (AssistantController.log.isDebugEnabled()) {
				AssistantController.log.debug(e.getMessage(), e);
			}
		}

		return jsonMap;
	}

	/**
	 * 实体与默认：验证
	 * 
	 * @param assistantVo
	 *            实体与默认视图对象
	 * @return 验证结果
	 */
	@RequestMapping(value = { "entityDefault/validationByCode.json" })
	@ResponseBody
	public Boolean entityDefaultValidationByCode(final AssistantVo assistantVo) {
		try {
			final int count = this.commonService.getAssistantService().readEntityDefaultByCodeNqId(assistantVo.getEntityDefault());

			return (count < 1);
		} catch (final Exception e) {
			if (AssistantController.log.isDebugEnabled()) {
				AssistantController.log.debug(e.getMessage(), e);
			}
		}

		return false;
	}

	/**
	 * 实体与默认：查看
	 * 
	 * @param assistantVo
	 *            实体与默认视图对象
	 * @param model
	 *            上下文映射
	 * @return 实体与默认查看编辑视图路径
	 */
	@RequestMapping(value = { "entityDefault/view.html" }, method = RequestMethod.GET)
	public String entityDefaultView(final AssistantVo assistantVo, final Model model) {
		final EntityDefaultDto entityDefault = this.commonService.getAssistantService().findEntityDefaultById(
		        assistantVo.getEntityDefault());

		model.addAttribute(AssistantController.models.entityDefault.name(), entityDefault);

		return AssistantController.Paths.ENTITY_DEFAULTView.path();
	}

	/**
	 * 实体扩展：删除
	 * 
	 * @param assistantVo
	 *            实体扩展视图对象
	 * @return 处理结果，json格式
	 */
	@RequestMapping(value = { "entityExtend/delete.json" }, method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> entityExtendDelete(final AssistantVo assistantVo) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.commonService.getAssistantService().deleteEntityExtendByIds(assistantVo.getEntityExtend());
		} catch (final Exception e) {
			jsonMap.put(Constants.message.stringValue(), e.getMessage());

			if (AssistantController.log.isDebugEnabled()) {
				AssistantController.log.debug(e.getMessage(), e);
			}
		}

		return jsonMap;
	}

	/**
	 * 实体扩展：编辑
	 * 
	 * @param assistantVo
	 *            实体扩展视图对象
	 * @param model
	 *            上下文映射
	 * @return 实体扩展编辑视图路径
	 */
	@RequestMapping(value = { "entityExtend/edit.html" }, method = RequestMethod.GET)
	public String entityExtendEdit(final AssistantVo assistantVo, final Model model) {
		final EntityExtendDto entityExtend = this.commonService.getAssistantService().findEntityExtendById(
		        assistantVo.getEntityExtend());

		model.addAttribute(AssistantController.models.entityExtend.name(), entityExtend);

		return AssistantController.Paths.ENTITY_EXTENDEdit.path();
	}

	/**
	 * 实体扩展：主界面
	 * 
	 * @return 实体扩展主界面视图路径
	 */
	@RequestMapping(value = { "entityExtend/index.html" })
	public String entityExtendIndex() {
		return AssistantController.Paths.ENTITY_EXTENDIndex.path();
	}

	/**
	 * 实体扩展：主界面分页查询
	 * 
	 * @param assistantVo
	 *            实体扩展视图对象
	 * @return 分页数据，json格式
	 */
	@RequestMapping(value = { "entityExtend/index/pagination.json" })
	@ResponseBody
	public Map<String, Object> entityExtendIndexPagination(final AssistantVo assistantVo) {
		final PaginationDto<EntityExtendDto> entityExtendPagination = this.commonService.getAssistantService()
		        .paginationEntityExtendByCode(assistantVo.getEntityExtend());

		final Map<String, Object> entityExtendPaginationMap = Maps.newHashMap();

		entityExtendPaginationMap.put(Constants.total.stringValue(), entityExtendPagination.getTotalSize());
		entityExtendPaginationMap.put(Constants.rows.stringValue(), entityExtendPagination.getDataList());

		return entityExtendPaginationMap;
	}

	/**
	 * 实体扩展：新增
	 * 
	 * @param assistantVo
	 *            实体扩展视图对象
	 * @param model
	 *            上下文映射
	 * @return 实体扩展编辑视图路径
	 */
	@RequestMapping(value = { "entityExtend/new.html" }, method = RequestMethod.GET)
	public String entityExtendNew(final AssistantVo assistantVo, final Model model) {
		model.addAttribute(AssistantController.models.entityExtend.name(), assistantVo.getEntityExtend());

		return AssistantController.Paths.ENTITY_EXTENDNew.path();
	}

	/**
	 * 实体扩展：保存
	 * 
	 * @param assistantVo
	 *            实体扩展视图对象
	 * @param bindingResult
	 *            绑定对象
	 * @return 处理结果，json格式
	 */
	@RequestMapping(value = { "entityExtend/save.txt" }, method = RequestMethod.POST)
	@ResponseBody
	public String entityExtendSave(final AssistantVo assistantVo, BindingResult bindingResult) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.entityExtendValidator.validate(assistantVo.getEntityExtend(), bindingResult);

			if (bindingResult.hasErrors()) {
				List<String> messageList = Lists.newArrayList();

				for (final ObjectError objectError : bindingResult.getAllErrors()) {
					messageList.add(objectError.getDefaultMessage());
				}

				String message = StringUtils.join(messageList, org.apache.commons.lang3.StringUtils.EMPTY);

				throw new Exception(message);
			}

			this.commonService.getAssistantService().saveEntityExtendByIdOrCode(assistantVo.getEntityExtend());
		} catch (final Exception e) {
			jsonMap.put(Constants.message.stringValue(), e.getMessage());

			if (AssistantController.log.isDebugEnabled()) {
				AssistantController.log.debug(e.getMessage(), e);
			}
		}

		return JSON.toJSONString(jsonMap);
	}

	/**
	 * 实体扩展：更新
	 * 
	 * @param assistantVo
	 *            实体扩展视图对象
	 * @return 处理结果，json格式
	 */
	@RequestMapping(value = { "entityExtend/update.json" }, method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> entityExtendUpdate(final AssistantVo assistantVo) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.commonService.getAssistantService().updateEntityExtendByIds(assistantVo.getEntityExtend());
		} catch (final Exception e) {
			jsonMap.put(Constants.message.stringValue(), e.getMessage());

			if (AssistantController.log.isDebugEnabled()) {
				AssistantController.log.debug(e.getMessage(), e);
			}
		}

		return jsonMap;
	}

	/**
	 * 实体扩展：验证
	 * 
	 * @param assistantVo
	 *            实体扩展视图对象
	 * @return 验证结果
	 */
	@RequestMapping(value = { "entityExtend/validationByCode.json" })
	@ResponseBody
	public Boolean entityExtendValidationByCode(final AssistantVo assistantVo) {
		try {
			final int count = this.commonService.getAssistantService().readEntityExtendByCodeNqId(assistantVo.getEntityExtend());

			return (count < 1);
		} catch (final Exception e) {
			if (AssistantController.log.isDebugEnabled()) {
				AssistantController.log.debug(e.getMessage(), e);
			}
		}

		return false;
	}

	/**
	 * 实体扩展：查看
	 * 
	 * @param assistantVo
	 *            实体扩展视图对象
	 * @param model
	 *            上下文映射
	 * @return 实体扩展查看编辑视图路径
	 */
	@RequestMapping(value = { "entityExtend/view.html" }, method = RequestMethod.GET)
	public String entityExtendView(final AssistantVo assistantVo, final Model model) {
		final EntityExtendDto entityExtend = this.commonService.getAssistantService().findEntityExtendById(
		        assistantVo.getEntityExtend());

		model.addAttribute(AssistantController.models.entityExtend.name(), entityExtend);

		return AssistantController.Paths.ENTITY_EXTENDView.path();
	}

	/**
	 * 实体日志：删除
	 * 
	 * @param assistantVo
	 *            实体日志视图对象
	 * @return 处理结果，json格式
	 */
	@RequestMapping(value = { "entityLog/delete.json" }, method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> entityLogDelete(final AssistantVo assistantVo) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.commonService.getAssistantService().deleteEntityLogByIds(assistantVo.getEntityLog());
		} catch (final Exception e) {
			jsonMap.put(Constants.message.stringValue(), e.getMessage());

			if (AssistantController.log.isDebugEnabled()) {
				AssistantController.log.debug(e.getMessage(), e);
			}
		}

		return jsonMap;
	}

	/**
	 * 实体日志：编辑
	 * 
	 * @param assistantVo
	 *            实体日志视图对象
	 * @param model
	 *            上下文映射
	 * @return 实体日志编辑视图路径
	 */
	@RequestMapping(value = { "entityLog/edit.html" }, method = RequestMethod.GET)
	public String entityLogEdit(final AssistantVo assistantVo, final Model model) {
		final EntityLogDto entityLog = this.commonService.getAssistantService().findEntityLogById(assistantVo.getEntityLog());

		model.addAttribute(AssistantController.models.entityLog.name(), entityLog);

		return AssistantController.Paths.ENTITY_LOGEdit.path();
	}

	/**
	 * 实体日志：主界面
	 * 
	 * @return 实体日志主界面视图路径
	 */
	@RequestMapping(value = { "entityLog/index.html" })
	public String entityLogIndex() {
		return AssistantController.Paths.ENTITY_LOGIndex.path();
	}

	/**
	 * 实体日志：主界面分页查询
	 * 
	 * @param assistantVo
	 *            实体日志视图对象
	 * @return 分页数据，json格式
	 */
	@RequestMapping(value = { "entityLog/index/pagination.json" })
	@ResponseBody
	public Map<String, Object> entityLogIndexPagination(final AssistantVo assistantVo) {
		final PaginationDto<EntityLogDto> entityLogPagination = this.commonService.getAssistantService().paginationEntityLogByCode(
		        assistantVo.getEntityLog());

		final Map<String, Object> entityLogPaginationMap = Maps.newHashMap();

		entityLogPaginationMap.put(Constants.total.stringValue(), entityLogPagination.getTotalSize());
		entityLogPaginationMap.put(Constants.rows.stringValue(), entityLogPagination.getDataList());

		return entityLogPaginationMap;
	}

	/**
	 * 实体日志：新增
	 * 
	 * @param assistantVo
	 *            实体日志视图对象
	 * @param model
	 *            上下文映射
	 * @return 实体日志编辑视图路径
	 */
	@RequestMapping(value = { "entityLog/new.html" }, method = RequestMethod.GET)
	public String entityLogNew(final AssistantVo assistantVo, final Model model) {
		model.addAttribute(AssistantController.models.entityLog.name(), assistantVo.getEntityLog());

		return AssistantController.Paths.ENTITY_LOGNew.path();
	}

	/**
	 * 实体日志：保存
	 * 
	 * @param assistantVo
	 *            实体日志视图对象
	 * @param bindingResult
	 *            绑定对象
	 * @return 处理结果，json格式
	 */
	@RequestMapping(value = { "entityLog/save.txt" }, method = RequestMethod.POST)
	@ResponseBody
	public String entityLogSave(final AssistantVo assistantVo, BindingResult bindingResult) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.entityLogValidator.validate(assistantVo.getEntityLog(), bindingResult);

			if (bindingResult.hasErrors()) {
				List<String> messageList = Lists.newArrayList();

				for (final ObjectError objectError : bindingResult.getAllErrors()) {
					messageList.add(objectError.getDefaultMessage());
				}

				String message = StringUtils.join(messageList, org.apache.commons.lang3.StringUtils.EMPTY);

				throw new Exception(message);
			}

			this.commonService.getAssistantService().saveEntityLogByIdOrCode(assistantVo.getEntityLog());
		} catch (final Exception e) {
			jsonMap.put(Constants.message.stringValue(), e.getMessage());

			if (AssistantController.log.isDebugEnabled()) {
				AssistantController.log.debug(e.getMessage(), e);
			}
		}

		return JSON.toJSONString(jsonMap);
	}

	/**
	 * 实体日志：更新
	 * 
	 * @param assistantVo
	 *            实体日志视图对象
	 * @return 处理结果，json格式
	 */
	@RequestMapping(value = { "entityLog/update.json" }, method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> entityLogUpdate(final AssistantVo assistantVo) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.commonService.getAssistantService().updateEntityLogByIds(assistantVo.getEntityLog());
		} catch (final Exception e) {
			jsonMap.put(Constants.message.stringValue(), e.getMessage());

			if (AssistantController.log.isDebugEnabled()) {
				AssistantController.log.debug(e.getMessage(), e);
			}
		}

		return jsonMap;
	}

	/**
	 * 实体日志：验证
	 * 
	 * @param assistantVo
	 *            实体日志视图对象
	 * @return 验证结果
	 */
	@RequestMapping(value = { "entityLog/validationByCode.json" })
	@ResponseBody
	public Boolean entityLogValidationByCode(final AssistantVo assistantVo) {
		try {
			final int count = this.commonService.getAssistantService().readEntityLogByCodeNqId(assistantVo.getEntityLog());

			return (count < 1);
		} catch (final Exception e) {
			if (AssistantController.log.isDebugEnabled()) {
				AssistantController.log.debug(e.getMessage(), e);
			}
		}

		return false;
	}

	/**
	 * 实体日志：查看
	 * 
	 * @param assistantVo
	 *            实体日志视图对象
	 * @param model
	 *            上下文映射
	 * @return 实体日志查看编辑视图路径
	 */
	@RequestMapping(value = { "entityLog/view.html" }, method = RequestMethod.GET)
	public String entityLogView(final AssistantVo assistantVo, final Model model) {
		final EntityLogDto entityLog = this.commonService.getAssistantService().findEntityLogById(assistantVo.getEntityLog());

		model.addAttribute(AssistantController.models.entityLog.name(), entityLog);

		return AssistantController.Paths.ENTITY_LOGView.path();
	}

	/**
	 * 实体关系：删除
	 * 
	 * @param assistantVo
	 *            实体关系视图对象
	 * @return 处理结果，json格式
	 */
	@RequestMapping(value = { "entityRelationship/delete.json" }, method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> entityRelationshipDelete(final AssistantVo assistantVo) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.commonService.getAssistantService().deleteEntityRelationshipByIds(assistantVo.getEntityRelationship());
		} catch (final Exception e) {
			jsonMap.put(Constants.message.stringValue(), e.getMessage());

			if (AssistantController.log.isDebugEnabled()) {
				AssistantController.log.debug(e.getMessage(), e);
			}
		}

		return jsonMap;
	}

	/**
	 * 实体关系：编辑
	 * 
	 * @param assistantVo
	 *            实体关系视图对象
	 * @param model
	 *            上下文映射
	 * @return 实体关系编辑视图路径
	 */
	@RequestMapping(value = { "entityRelationship/edit.html" }, method = RequestMethod.GET)
	public String entityRelationshipEdit(final AssistantVo assistantVo, final Model model) {
		final EntityRelationshipDto entityRelationship = this.commonService.getAssistantService().findEntityRelationshipById(
		        assistantVo.getEntityRelationship());

		model.addAttribute(AssistantController.models.entityRelationship.name(), entityRelationship);

		return AssistantController.Paths.ENTITY_RELATIONSHIPEdit.path();
	}

	/**
	 * 实体关系：主界面
	 * 
	 * @return 实体关系主界面视图路径
	 */
	@RequestMapping(value = { "entityRelationship/index.html" })
	public String entityRelationshipIndex() {
		return AssistantController.Paths.ENTITY_RELATIONSHIPIndex.path();
	}

	/**
	 * 实体关系：主界面分页查询
	 * 
	 * @param assistantVo
	 *            实体关系视图对象
	 * @return 分页数据，json格式
	 */
	@RequestMapping(value = { "entityRelationship/index/pagination.json" })
	@ResponseBody
	public Map<String, Object> entityRelationshipIndexPagination(final AssistantVo assistantVo) {
		final PaginationDto<EntityRelationshipDto> entityRelationshipPagination = this.commonService.getAssistantService()
		        .paginationEntityRelationshipByCode(assistantVo.getEntityRelationship());

		final Map<String, Object> entityRelationshipPaginationMap = Maps.newHashMap();

		entityRelationshipPaginationMap.put(Constants.total.stringValue(), entityRelationshipPagination.getTotalSize());
		entityRelationshipPaginationMap.put(Constants.rows.stringValue(), entityRelationshipPagination.getDataList());

		return entityRelationshipPaginationMap;
	}

	/**
	 * 实体关系：新增
	 * 
	 * @param assistantVo
	 *            实体关系视图对象
	 * @param model
	 *            上下文映射
	 * @return 实体关系编辑视图路径
	 */
	@RequestMapping(value = { "entityRelationship/new.html" }, method = RequestMethod.GET)
	public String entityRelationshipNew(final AssistantVo assistantVo, final Model model) {
		model.addAttribute(AssistantController.models.entityRelationship.name(), assistantVo.getEntityRelationship());

		return AssistantController.Paths.ENTITY_RELATIONSHIPNew.path();
	}

	/**
	 * 实体关系：保存
	 * 
	 * @param assistantVo
	 *            实体关系视图对象
	 * @param bindingResult
	 *            绑定对象
	 * @return 处理结果，json格式
	 */
	@RequestMapping(value = { "entityRelationship/save.txt" }, method = RequestMethod.POST)
	@ResponseBody
	public String entityRelationshipSave(final AssistantVo assistantVo, BindingResult bindingResult) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.entityRelationshipValidator.validate(assistantVo.getEntityRelationship(), bindingResult);

			if (bindingResult.hasErrors()) {
				List<String> messageList = Lists.newArrayList();

				for (final ObjectError objectError : bindingResult.getAllErrors()) {
					messageList.add(objectError.getDefaultMessage());
				}

				String message = StringUtils.join(messageList, org.apache.commons.lang3.StringUtils.EMPTY);

				throw new Exception(message);
			}

			this.commonService.getAssistantService().saveEntityRelationshipByIdOrCode(assistantVo.getEntityRelationship());
		} catch (final Exception e) {
			jsonMap.put(Constants.message.stringValue(), e.getMessage());

			if (AssistantController.log.isDebugEnabled()) {
				AssistantController.log.debug(e.getMessage(), e);
			}
		}

		return JSON.toJSONString(jsonMap);
	}

	/**
	 * 实体关系：更新
	 * 
	 * @param assistantVo
	 *            实体关系视图对象
	 * @return 处理结果，json格式
	 */
	@RequestMapping(value = { "entityRelationship/update.json" }, method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> entityRelationshipUpdate(final AssistantVo assistantVo) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.commonService.getAssistantService().updateEntityRelationshipByIds(assistantVo.getEntityRelationship());
		} catch (final Exception e) {
			jsonMap.put(Constants.message.stringValue(), e.getMessage());

			if (AssistantController.log.isDebugEnabled()) {
				AssistantController.log.debug(e.getMessage(), e);
			}
		}

		return jsonMap;
	}

	/**
	 * 实体关系：验证
	 * 
	 * @param assistantVo
	 *            实体关系视图对象
	 * @return 验证结果
	 */
	@RequestMapping(value = { "entityRelationship/validationByCode.json" })
	@ResponseBody
	public Boolean entityRelationshipValidationByCode(final AssistantVo assistantVo) {
		try {
			final int count = this.commonService.getAssistantService().readEntityRelationshipByCodeNqId(
			        assistantVo.getEntityRelationship());

			return (count < 1);
		} catch (final Exception e) {
			if (AssistantController.log.isDebugEnabled()) {
				AssistantController.log.debug(e.getMessage(), e);
			}
		}

		return false;
	}

	/**
	 * 实体关系：查看
	 * 
	 * @param assistantVo
	 *            实体关系视图对象
	 * @param model
	 *            上下文映射
	 * @return 实体关系查看编辑视图路径
	 */
	@RequestMapping(value = { "entityRelationship/view.html" }, method = RequestMethod.GET)
	public String entityRelationshipView(final AssistantVo assistantVo, final Model model) {
		final EntityRelationshipDto entityRelationship = this.commonService.getAssistantService().findEntityRelationshipById(
		        assistantVo.getEntityRelationship());

		model.addAttribute(AssistantController.models.entityRelationship.name(), entityRelationship);

		return AssistantController.Paths.ENTITY_RELATIONSHIPView.path();
	}

	/**
	 * 实体树：删除
	 * 
	 * @param assistantVo
	 *            实体树视图对象
	 * @return 处理结果，json格式
	 */
	@RequestMapping(value = { "entityTree/delete.json" }, method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> entityTreeDelete(final AssistantVo assistantVo) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.commonService.getAssistantService().deleteEntityTreeByIds(assistantVo.getEntityTree());
		} catch (final Exception e) {
			jsonMap.put(Constants.message.stringValue(), e.getMessage());

			if (AssistantController.log.isDebugEnabled()) {
				AssistantController.log.debug(e.getMessage(), e);
			}
		}

		return jsonMap;
	}

	/**
	 * 实体树：编辑
	 * 
	 * @param assistantVo
	 *            实体树视图对象
	 * @param model
	 *            上下文映射
	 * @return 实体树编辑视图路径
	 */
	@RequestMapping(value = { "entityTree/edit.html" }, method = RequestMethod.GET)
	public String entityTreeEdit(final AssistantVo assistantVo, final Model model) {
		final EntityTreeDto entityTree = this.commonService.getAssistantService().findEntityTreeById(assistantVo.getEntityTree());

		model.addAttribute(AssistantController.models.entityTree.name(), entityTree);

		return AssistantController.Paths.ENTITY_TREEEdit.path();
	}

	/**
	 * 实体树：主界面
	 * 
	 * @return 实体树主界面视图路径
	 */
	@RequestMapping(value = { "entityTree/index.html" })
	public String entityTreeIndex() {
		return AssistantController.Paths.ENTITY_TREEIndex.path();
	}

	/**
	 * 实体树：主界面分页查询
	 * 
	 * @param assistantVo
	 *            实体树视图对象
	 * @return 分页数据，json格式
	 */
	@RequestMapping(value = { "entityTree/index/pagination.json" })
	@ResponseBody
	public Map<String, Object> entityTreeIndexPagination(final AssistantVo assistantVo) {
		final PaginationDto<EntityTreeDto> entityTreePagination = this.commonService.getAssistantService().paginationEntityTreeByCode(
		        assistantVo.getEntityTree());

		final Map<String, Object> entityTreePaginationMap = Maps.newHashMap();

		entityTreePaginationMap.put(Constants.total.stringValue(), entityTreePagination.getTotalSize());
		entityTreePaginationMap.put(Constants.rows.stringValue(), entityTreePagination.getDataList());

		return entityTreePaginationMap;
	}

	/**
	 * 实体树：新增
	 * 
	 * @param assistantVo
	 *            实体树视图对象
	 * @param model
	 *            上下文映射
	 * @return 实体树编辑视图路径
	 */
	@RequestMapping(value = { "entityTree/new.html" }, method = RequestMethod.GET)
	public String entityTreeNew(final AssistantVo assistantVo, final Model model) {
		model.addAttribute(AssistantController.models.entityTree.name(), assistantVo.getEntityTree());

		return AssistantController.Paths.ENTITY_TREENew.path();
	}

	/**
	 * 实体树：保存
	 * 
	 * @param assistantVo
	 *            实体树视图对象
	 * @param bindingResult
	 *            绑定对象
	 * @return 处理结果，json格式
	 */
	@RequestMapping(value = { "entityTree/save.txt" }, method = RequestMethod.POST)
	@ResponseBody
	public String entityTreeSave(final AssistantVo assistantVo, BindingResult bindingResult) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.entityTreeValidator.validate(assistantVo.getEntityTree(), bindingResult);

			if (bindingResult.hasErrors()) {
				List<String> messageList = Lists.newArrayList();

				for (final ObjectError objectError : bindingResult.getAllErrors()) {
					messageList.add(objectError.getDefaultMessage());
				}

				String message = StringUtils.join(messageList, org.apache.commons.lang3.StringUtils.EMPTY);

				throw new Exception(message);
			}

			this.commonService.getAssistantService().saveEntityTreeByIdOrCode(assistantVo.getEntityTree());
		} catch (final Exception e) {
			jsonMap.put(Constants.message.stringValue(), e.getMessage());

			if (AssistantController.log.isDebugEnabled()) {
				AssistantController.log.debug(e.getMessage(), e);
			}
		}

		return JSON.toJSONString(jsonMap);
	}

	/**
	 * 实体树：更新
	 * 
	 * @param assistantVo
	 *            实体树视图对象
	 * @return 处理结果，json格式
	 */
	@RequestMapping(value = { "entityTree/update.json" }, method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> entityTreeUpdate(final AssistantVo assistantVo) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.commonService.getAssistantService().updateEntityTreeByIds(assistantVo.getEntityTree());
		} catch (final Exception e) {
			jsonMap.put(Constants.message.stringValue(), e.getMessage());

			if (AssistantController.log.isDebugEnabled()) {
				AssistantController.log.debug(e.getMessage(), e);
			}
		}

		return jsonMap;
	}

	/**
	 * 实体树：验证
	 * 
	 * @param assistantVo
	 *            实体树视图对象
	 * @return 验证结果
	 */
	@RequestMapping(value = { "entityTree/validationByCode.json" })
	@ResponseBody
	public Boolean entityTreeValidationByCode(final AssistantVo assistantVo) {
		try {
			final int count = this.commonService.getAssistantService().readEntityTreeByCodeNqId(assistantVo.getEntityTree());

			return (count < 1);
		} catch (final Exception e) {
			if (AssistantController.log.isDebugEnabled()) {
				AssistantController.log.debug(e.getMessage(), e);
			}
		}

		return false;
	}

	/**
	 * 实体树：查看
	 * 
	 * @param assistantVo
	 *            实体树视图对象
	 * @param model
	 *            上下文映射
	 * @return 实体树查看编辑视图路径
	 */
	@RequestMapping(value = { "entityTree/view.html" }, method = RequestMethod.GET)
	public String entityTreeView(final AssistantVo assistantVo, final Model model) {
		final EntityTreeDto entityTree = this.commonService.getAssistantService().findEntityTreeById(assistantVo.getEntityTree());

		model.addAttribute(AssistantController.models.entityTree.name(), entityTree);

		return AssistantController.Paths.ENTITY_TREEView.path();
	}
}
